<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Payment_gateway_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $gateway ="";
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$ci= & get_instance();
		$ci->db->set_dbprefix($this->current_dbprefix);
	}
	
	function setId($input){
		$this->id=$input;
	}
	function setGateway($input){
		$this->gateway=$input;
	}
	function getId(){
		return $this->id;
	}
	function getGateway(){
		return $this->gateway;
	}
	
	function init($cmd, $input)
	{
		$ci=& get_instance();
		if($cmd == 'id'){
			$result = $ci->db->where('id',$input)->get(TBL_PAYMENT_GATEWAY)->result();
		}else if($cmd == 'gateway'){
			$result = $ci->db->where('gateway',$input)->get(TBL_PAYMENT_GATEWAY)->result();
		}else{
			return false;
		}
		if( !empty($result) ){
			$this->id = $result[0]->id;
			$this->gateway = $result[0]->gateway;
			return $this;
		}
		return false;
	}
	function getPaymentGatewayList(){
		$ci=& get_instance();
		return $ci->db->get(TBL_PAYMENT_GATEWAY)->result();
	}
	function getPaymentGatewayNameByIds($ids){
		$ci=& get_instance();
		$ci->db->where_in('id', explode(',',$ids));
		$resultList = $ci->db->get(TBL_PAYMENT_GATEWAY)->result();
		$nameString = '';
		if($resultList){
			foreach ($resultList as $result ){
				$nameString .= $result->gateway.',<br/>';
			}
			$nameString = substr($nameString, 0, strlen($nameString) - 6);
		}
		return $nameString;
	}
	function save(){
		$ci=& get_instance();
		$ci->db->set('gateway',$this->getGateway());
		$ci->db->insert(TBL_PAYMENT_GATEWAY);
		return $ci->db->insert_id();
	}
	function update(){
		$ci=& get_instance();
		$ci->db->set('gateway', $this->gateway);
		return $ci->db->where('id', $this->id)->update(TBL_PAYMENT_GATEWAY);
	}
	function delete($id){
		$ci=& get_instance();
		$ci->db->where('id',$id);
		$ci->db->delete(TBL_PAYMENT_GATEWAY);
	}
	function checkExistedName($id, $name){
		$ci=& get_instance();
		if($id){
			$ci->db->where('id !=',$id);
		}
		$ci->db->where('gateway',$name);
		return $ci->db->get(TBL_PAYMENT_GATEWAY)->result();
	}
}